using net_work.Config;
using net_work.Data.DTO.Cou;
using net_work.Data.Entity.Cou;
using net_work.IRepository.Cou;

namespace net_work.Repository.Cou;

public class ScheduleRepository(ApplicationDbContext dbContext) : Repository<Schedule>(dbContext), IScheduleRepository
{
    public async Task<long> InsertAsync(ScheduleAddRequest scheduleAddRequest)
    {
        var newSchedule = new Schedule
        {
            WeekDay = scheduleAddRequest.WeekDay,
            ClassNumbers = scheduleAddRequest.ClassNumbers,
            TeachId = scheduleAddRequest.TeachId,
            Remark = scheduleAddRequest.Remark
        };
        await dbContext.Schedules.AddAsync(newSchedule);
        await dbContext.SaveChangesAsync();
        return newSchedule.Id;
    }

    public async Task<bool> UpdateAsync(ScheduleUpdateRequest scheduleUpdateRequest)
    {
        var schedule = await dbContext.Schedules.FindAsync(scheduleUpdateRequest.Id);
        if (schedule == null)
        {
            return false;
        }
        
        schedule.WeekDay = scheduleUpdateRequest.WeekDay;
        schedule.ClassNumbers = scheduleUpdateRequest.ClassNumbers;
        schedule.TeachId = scheduleUpdateRequest.TeachId;
        schedule.Remark = scheduleUpdateRequest.Remark;

        await dbContext.SaveChangesAsync();
        return true;
    }
}